<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;

use App\Category;

class CategoryController extends Controller
{
    //定义数组
    protected static $field = [
        ['id'=>1,'searchtype'=>'编号'],
        ['id'=>2,'searchtype'=>'权重'],
        ['id'=>3,'searchtype'=>'创建时间'],
        ['id'=>4,'searchtype'=>'更新时间'],
        ['id'=>5,'searchtype'=>'分类名称'],
        ['id'=>6,'searchtype'=>'一级分类'],
    ];

    //分类列表
    public function index() {
        $search = Input::get('searchtype');
        $param = Input::get('param');
        $obj = new Category;
        if($search == 1){
            $obj = $obj->where('id','like',"%$param%");
        }elseif ($search == 2){
            $obj = $obj->where('rank','like',"%$param%");
        }elseif ($search == 3){
            $obj = $obj->where('create_time','like',"%$param%");
        }elseif ($search == 4){
            $obj = $obj->where('update_time','like',"%$param%");
        }elseif ($search == 5){
            $obj = $obj->where('name','like',"%$param%");
        }elseif ($search == 6){
            $obj = $obj->where('parent_id','like',"%$param%");
        }
        $list = $obj->Paginate(10);
        if($list == false && !empty($search)){
            return back()->withErrors('未搜索到结果！');
        }
        $searchtype = self::$field;
        return view('admin.category.index',['list'=>$list,'searchtype'=>$searchtype]);


//        $list = Category::all()->toArray();
//        return view('admin.category.index',['list'=>$list]);
    }

    //进入添加页面
    public function add() {
        $list = Category::all()->toArray();
        return view('admin.category.addCategory',['list'=>$list]);
    }

    //添加分类
    public function addCategory() {
        $name = input::get('name');
        $parent = input::get('parent');
        if(empty($name) || $parent == null) {
            return redirect()->back()->withInput()->withErrors('缺少参数!');
        }

        $cate= Category::where('name',$name)->first();
        if(empty($cate)) {
            $list = Category::insert([
                'name'=>$name,
                'parent_id'=>$parent,
            ]);
            if($list) {
                return Redirect::to("admin/categoryList")->withErrors('添加成功！');
            }
            else{
                return redirect()->back()->withErrors('添加失败！');

            }
        }
        else {
            return redirect()->back()->withErrors('该分类已存在！');
        }
    }

    //删除分类
    public function delCate($id) {
        //echo "hello";
        $list = Category::where('id',$id )->first();
        if($list['parent_id'] != 0) {
            $del = Category::where('id',$id)->delete();
            if($del) {
                return Redirect::to("admin/categoryList")->withErrors('删除成功！');
            }
            else {
                return  redirect()->back()->withInput()->withErrors('删除失败！');
            }
        }
        else {
            $del = Category::where('parent_id', $id)->delete();
            $del1 = Category::where('id', $id)->delete();
            if($del and $del1) {
                return Redirect::to("admin/categoryList")->withErrors('删除成功！');
            }
            else {
                return  redirect()->back()->withInput()->withErrors('删除失败！');
            }
        }
    }

    //获取要修改的分类
    public function getCate($id) {
        $cate = Category::where('id',$id)->first()->toArray();
        $list = Category::all()->toArray();
        if($cate) {
            return view('admin.category.updateCategory',['cate'=>$cate,'list'=>$list]);
        }
        else {
            return redirect()->back()->withInput()->withErrors('载入失败!');
        }
    }

    //更新分类
    public function updateCate() {
        $id = input::get('id');
        $data['name'] = input::get('name');
        $data['parent_id'] = input::get('parent');

        if(empty($data['name']) || empty($id) || $data['parent_id'] == null) {
            return redirect()->back()->withInput()->withErrors('缺少参数!');
        }
        $cate= Category::where('id',$id)->update($data);
        if($cate) {
            return redirect::to("admin/categoryList")->withErrors('更新成功！');
        }
        else {
            return redirect()->back()->withInput()->withErrores('更新失败!');
        }
    }

}
